import store from 'store';
export function login() {
 return new Promise(function (resolve, reject) {
    uni.login({
      success(res) {
		  console.log(store.state.token, 'aaaaaaaaaaaaaab');
        if (res.code) {
          // 登录请求
		  console.log(store.state.token, 'aaaaaaaaaaaaaaaaaaaaaaa');
          uni.request({
            url: store.state.URL + 'login/login/mini_login',
			// url : 'http://rwd.zanboon.com/login/login/mini_login',
            method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
			},
            data: {
              code: res.code
            },
            success(res) {
              uni.hideLoading();
			  //console.log(res);
              switch (res.data.flag) {
                case 0:
                  // 0未注册，去注册
                  register();/* .catch(err=> {
										uni.showToast({
											icon: 'none',
											title: '失败！请重新尝试',
											duration: 2000
										})
                  }) */
                  resolve(res)
                  break;
                case 1:
				//console.log(res);
                  // 1登录成功，缓存用户信息，token
                  uni.setStorageSync('token',res.data.token);
				  //console.log(res.data.token);
                  uni.setStorageSync('userInfo', {
                    nickName: res.data.baidu_user.nickName,
                    avatarUrl: res.data.baidu_user.avatarUrl
                  })
                  // 判断数据是否在登录之前返回
                  if (getApp().userInfoReadyCallback) {
                    getApp().userInfoReadyCallback()
                  }
                  resolve(res)
                  break;
                case 2:
                  // 2登录成功，没有token
				  
						uni.showToast({
							icon: 'none',
							title: '失败！请重新尝试',
							duration: 2000
						})
                  resolve(res)
                  break;
                default:
                  // uni.showToast.fail('登录失败，请重试')
						uni.showToast({
							icon: 'none',
							title: '失败！请重新尝试',
							duration: 2000
						})
									
                  reject(res)
                  break;
              }
            },
            fail(err) {
              uni.hideToast();
              // uni.showToast.fail('登录失败，请重试')
					uni.showToast({
						icon: 'none',
						title: '失败！请重新尝试',
						duration: 2000
					})
              reject(err)
            }
          })
        } else {
          reject('登录失败!获取登录码失败')
        }
      }
    })
  })
}
export function register() {
  return new Promise((resolve, reject) => {
    // 获取用户码
    swan.login({
      success(codes) {
        // 获取用户信息
        swan.getUserInfo({
          withCredentials: true,
          success(info) {
            let code = codes.code
            let iv = info.iv;
            let encryptedData = info.data;
            // 注册请求
			console.log(encryptedData);
            swan.request({
              url: store.state.URL + 'login/login/mini_register',
              //url: 'http://rwd.zanboon.com/login/login/mini_register',
              data: {
                code,
                iv,
                encryptedData
              },
              method: 'POST',
              success(res) {
                uni.hideLoading()
                switch (res.data.flag) {
                  case 0:
                    // 授权失败
                    // uni.showToast.fail('授权失败！请打开授权')
                    reject(res)
                    break;
                  case 1:
                    // 注册成功
					console.log('注册成功');
                    login().then(()=>{
                      swan.navigateBack({
                        delta:1
                        })
                    }).catch(err => {
                      // uni.showToast.fail('失败！请重新尝试')
											uni.showToast({
												icon: 'none',
												title: '失败！请重试',
												duration: 2000
											})
                    });
                    resolve(res)
                    break;
                  case 2:
                    // 已注册
                    login().then(() => {
                      swan.navigateBack({
                        delta: 1
                      })
                      }).catch(err => {
                        // uni.showToast.fail('失败！请重新尝试')
							uni.showToast({
								icon: 'none',
								title: '失败！请重试',
								duration: 2000
							})
                      })
                    resolve(res)
                    break;
                  default:
                    // uni.showToast.fail('注册失败！请重试')
						uni.showToast({
							icon: 'none',
							title: '注册失败！请重试',
							duration: 2000
						})
                    reject(res)
                    break;
                }
              },
              fail() {
                // uni.showToast.fail('注册失败！请重试')
					uni.showToast({
						icon: 'none',
						title: '注册失败！请重试',
						duration: 2000
					})
              }
            })
          },
          fail(err) {
            reject(err, '注册获取用户信息失败')
          }
        })
      }
    })
  })
}
export function is_login(res){
	if(res.data.status == false){
		if(res.data.msg = "Token不可用"){
			console.log('请先登录');
			uni.showToast({
				title: '请先登录',
				icon:'none',
				duration: 1000
			});
		}else{
			console.log(res.data.msg);
			uni.showToast({
				title: res.data.msg,
				icon:'none',
				duration: 2000
			});
		}
	}
	if(res.data.status == true){
		/* uni.showToast({
			title: res.data.msg+'aaa',
			icon:'success',
			duration: 2000
		}); */
	}
}